/**
 * 为各个按钮添加click功能
 */
window.onload = function () {
    document.getElementById("add-left").onclick = addLeft;
    document.getElementById("add-right").onclick = addRight;
    document.getElementById("del-left").onclick = delLeft;
    document.getElementById("del-right").onclick = delRight;
    document.getElementById("inquiry").onclick = inquiry;
    document.getElementById("clear").onclick = clear;
}

/**
 * 左侧添加数字
 */
function addLeft() {
    var text = document.getElementById("txt").value;
    var re = /[\s+,\,,，,、,;,；]+/;
    var arr = text.split(re);
    for (var i = 0; i < arr.length; i++) {
        var node_div = document.createElement("div");
        var node_container = document.getElementById("container");
        var node_text = document.createTextNode(arr[i]);
        node_div.appendChild(node_text);
        node_div.className = "number";
        if (!node_container.childNodes) {
            node_container.appendChild(node_div);
        } else {
            var fir_node = node_container.firstChild;
            node_container.insertBefore(node_div, fir_node);
        }
    }

}

/**
 * 右侧添加数字
 */
function addRight() {
    var text = document.getElementById("txt").value;
    var re = /[\s+,\,,，,、,;,；]+/;
    var arr = text.split(re);
    for (var i = 0; i < arr.length; i++) {
        var node_text = document.createTextNode(arr[i]);
        var node_div = document.createElement("div");
        var node_container = document.getElementById("container");
        node_div.appendChild(node_text);
        node_div.className = "number";
        if (!node_container.childNodes) {
            node_container.appendChild(node_div);
        } else {
            node_container.appendChild(node_div);
        }
    }

}
/**
 * 删除左侧数字
 */
function delLeft() {
    var node_container = document.getElementById("container");
    if (node_container.firstChild) {
        var node_fir = node_container.firstChild;
        node_container.removeChild(node_fir);
    }
}

/**
 * 删除右侧数字
 */
function delRight() {
    var node_container = document.getElementById("container");
    if (node_container.lastChild) {
        var node_last = node_container.lastChild;
        node_container.removeChild(node_last);
    }
}
//查询
function inquiry() {
    var inquiry_content = document.getElementById("inquiry-text").value;
    if (inquiry_content) {
        var str = "new RegExp('" + document.getElementById("inquiry-text").value + "')";
        var re = eval(str);
        var ch = document.getElementById("container").children;
        for (var i = 0; i < ch.length; i++) {
            if (re.test(ch[i].innerText)) {
                ch[i].style.color = "#000";
            }
        }
    }
}
//清除
function clear() {
    var children = document.getElementById("container").children;
    for (var i = 0; i < children.length; i++) {
        children[i].style.color = "#fff";
    }
}
